package com.bingo.batch;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class BatchProducer {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    public void send(List<String> payloads) {
        if (!CollectionUtils.isEmpty(payloads)) {
            List<Message<String>> messageList = new ArrayList<>();
            for (String payload : payloads) {
                messageList.add(MessageBuilder.withPayload(payload).build());
            }
            SendResult sendResult = rocketMQTemplate.syncSend("Base:springboot-batch", messageList, 60000L);
            System.out.println("sendResult=" +  sendResult);
        }
    }
}
